package com.right.dao.impl;

import java.util.List;

import com.Tools.DBTools;
import com.Tools.MD5Util;
import com.right.dao.ManagerDao;
import com.right.entity.Manager;
import com.right.entity.Users;

public class ManagerDaoImpl implements ManagerDao {
	MD5Util md5 = new MD5Util();
	
	@Override
	public boolean login(String name, String pwd) {
		String sql = "select count(id) from manager where name = ? and pwd = ?";
		String md5pwd = md5.getEncodeByMd5(pwd);
		List<Users> list = (List<Users>)DBTools.exQuery(sql, Manager.class, name,md5pwd);
		return list.size()>0;
	}

	@Override
	public boolean addManager(Manager manager) {
		String sql = "insert into manager values(null,?,?)";
		String pwd = manager.getPwd();
		String md5pwd =md5.getEncodeByMd5(pwd);
		return DBTools.exUpdate(sql,manager.getName(),md5pwd) > 0;
	}

	@Override
	public boolean updateManager(Manager manager) {
		String sql = "update manager set name = ?,pwd = ? where id = ?";
		return DBTools.exUpdate(sql,manager.getName(),manager.getPwd(),manager.getId())>0;
	}
	
	@Override
	public Manager getUserByName(String managername) {
		String sql = "select * from manager where name = ?";
		List<Manager> list = (List<Manager>)DBTools.exQuery(sql, Manager.class, managername);
		if (list.size()==0) {
			return null;
		}
		return list.get(0);
	}
}
